package com.hiznu.amap_flutter

import android.content.Context
import android.content.pm.PackageManager
import com.amap.api.location.AMapLocationClient
import com.amap.api.maps.MapsInitializer
import com.hiznu.amap_flutter.AmapFlutterPlugin.Companion.flEventChannel
import com.hiznu.amap_flutter.map.AMapPlatformViewFactory
import fl.channel.FlChannelPlugin
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.security.MessageDigest
import java.util.Locale

class AMapMapHandler(plugin: FlutterPlugin.FlutterPluginBinding) :
    MethodChannel.MethodCallHandler {

    private var channel: MethodChannel =
        MethodChannel(plugin.binaryMessenger, "amap_flutter/map")

    private val context = plugin.applicationContext

    init {
        channel.setMethodCallHandler(this)
        plugin.platformViewRegistry.registerViewFactory(
            "amap_map", AMapPlatformViewFactory(plugin.binaryMessenger)
        )
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        when (call.method) {
            "setApiKey" -> {
                val key = call.argument<String>("key")!!
                val isAgree = call.argument<Boolean>("isAgree")!!
                val isContains = call.argument<Boolean>("isContains")!!
                val isShow = call.argument<Boolean>("isShow")!!
                val enableHTTPS = call.argument<Boolean>("enableHTTPS")!!
                AMapLocationClient.updatePrivacyAgree(context, isAgree)
                AMapLocationClient.updatePrivacyShow(context, isContains, isShow)
                AMapLocationClient.setApiKey(key)
                MapsInitializer.setApiKey(key)
                MapsInitializer.initialize(context)
                MapsInitializer.setProtocol(if (enableHTTPS) MapsInitializer.HTTPS else MapsInitializer.HTTP)
                MapsInitializer.updatePrivacyAgree(context, isAgree)
                MapsInitializer.updatePrivacyShow(context, isContains, isShow)
                flEventChannel = FlChannelPlugin.getEventChannel("amap_flutter/map_event")
                result.success(true)
            }

            "sha1" -> {
                result.success(sHA1(context))
            }
        }
    }

    fun sHA1(context: Context): String? {
        return try {
            val info = context.packageManager.getPackageInfo(
                context.packageName, PackageManager.GET_SIGNATURES
            )
            val cert = info.signatures!!.get(0).toByteArray()
            val md = MessageDigest.getInstance("SHA1")
            val publicKey = md.digest(cert)
            val hexString = StringBuffer()
            for (ele in publicKey) {
                val appendString = Integer.toHexString(0xFF and ele.toInt())
                    .toUpperCase(Locale.US)
                if (appendString.length == 1)
                    hexString.append("0")
                hexString.append(appendString)
                hexString.append(":")
            }
            val result = hexString.toString()
            return result.substring(0, result.length - 1)
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        }
    }

    fun detached() {
        channel.setMethodCallHandler(null)
    }
}